package com.common.controller;

import com.common.annotation.LogAnnotation;
import com.common.controller.base.BaseAjaxController;
import com.common.dto.PersonInfoDTO;
import com.common.service.PersonInfoService;
import com.common.util.ConstantDefine;
import com.common.util.LogType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/personInfo")
public class PersonInfoController extends BaseAjaxController<PersonInfoDTO> {

    @Autowired
    private PersonInfoService personInfoService;


    /**
     * 根据id查询账户信息
     */
    @ResponseBody
    @RequestMapping(value = "/selectById")
    public PersonInfoDTO selectById(PersonInfoDTO personInfoDTO) {
        PersonInfoDTO personInfo = personInfoService.selectById(personInfoDTO);
        return personInfo;
    }

    /**
     * 添加账户信息
     */
    @Override
    public ResponseEntity<Map<String, Object>> save(PersonInfoDTO entity, BindingResult bindingResult) {
        return null;
    }

    /**
     * 添加账户信息
     */
    @ResponseBody
    @RequestMapping("/save")
    @LogAnnotation(description = "添加用户", bizID = LogType.ACCOUNT)
    public Map<String, Object> saveMessage(PersonInfoDTO entity) {
        Map<String, Object> map = personInfoService.saveMessage(entity);
        return map;
    }

    /**
     * 修改用户状态
     */
    @ResponseBody
    @RequestMapping("/startORlock")
    @LogAnnotation(description = "锁定、解锁用户", bizID = LogType.ACCOUNT)
    public Map<String, Object> updateStartORlock(PersonInfoDTO personInfoDTO) {
        Map<String, Object> result = personInfoService.updateState(personInfoDTO);
        return result;
    }


    @Override
    @RequestMapping(value = "/updateInit/{id}")
    public PersonInfoDTO updateInit(@PathVariable("id") String id) {
        PersonInfoDTO personInfoDTO = personInfoService.findByID(id);
        return personInfoDTO;
    }

    @Override
    public ResponseEntity<Map<String, Object>> update(PersonInfoDTO entity, BindingResult bindingResult) {
        return null;
    }

    /**
     * 更新用户
     *
     * @param entity
     * @return
     */
    @ResponseBody
    @RequestMapping(value = ConstantDefine.UPDATE_REQ)
    @LogAnnotation(description = "更新修改用户信息", bizID = LogType.ACCOUNT)
    public Map<String, Object> updateMessage(PersonInfoDTO entity) {
        Map<String, Object> map = personInfoService.updateMessage(entity);
        return map;
    }

    /**
     * Created by wrl on 2017/07/14
     * 软删除
     *
     * @param entity
     * @return
     */
    @RequestMapping(value = "/deleteByIds", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> deleteByIds(PersonInfoDTO entity) {
        List<String> ids = entity.getIds();
        Map<String, Object> map = personInfoService.deletePersons(ids);
        return map;
    }

    /**
     * Created by wrl on 2017/07/14
     * 软删除:state=0
     *
     * @param entity
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "deleteById", method = RequestMethod.POST)
    public Map<String, Object> deleteById(PersonInfoDTO entity) {
        Map<String, Object> map = personInfoService.deletePerson(entity);
        return map;
    }

    @Override
    public ResponseEntity<Map<String, Object>> delete(List<String> id) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String init(Model model) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String init(PersonInfoDTO entity, Model model) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Object queryByEntity(PersonInfoDTO entity) {
        // TODO Auto-generated method stub
        return null;
    }

    /**
     * 分页查询
     *
     * @param entity
     * @return
     */
    @ResponseBody
    @RequestMapping("/selectPaging")
    @LogAnnotation(description = "用户管理页面", bizID = LogType.ACCOUNT)
    public Map<String, Object> selectByPaging(PersonInfoDTO entity) {
        Map<String, Object> map = new HashMap<>();
        if (entity.getIsSearch().equals("1"))
            map = personInfoService.searchPaging(entity);
        else
            map = personInfoService.selectByPaging(entity);
        return map;
    }

}
